package com.carrentalplatform.controller;

import com.carrentalplatform.model.User;
import com.carrentalplatform.model.Vehicle;
import com.carrentalplatform.model.RentalOrder;
import com.carrentalplatform.model.CarRentalShop;
import com.carrentalplatform.service.UserService;
import com.carrentalplatform.service.VehicleService;
import com.carrentalplatform.service.RentalOrderService;
import com.carrentalplatform.service.CarRentalShopService;
import com.carrentalplatform.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private VehicleService vehicleService;
    
    @Autowired
    private RentalOrderService rentalOrderService;
    
    @Autowired
    private CarRentalShopService carRentalShopService;
    
    // User management
    @GetMapping("/users")
    public Result<List<User>> getAllUsers() {
        return Result.success(userService.getAllUsers());
    }
    
    @GetMapping("/users/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        return Result.success(userService.findById(id));
    }
    
    @PutMapping("/users/{id}")
    public Result<Boolean> updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return Result.success(userService.updateUser(user));
    }
    
    @DeleteMapping("/users/{id}")
    public Result<Boolean> deleteUser(@PathVariable Long id) {
        return Result.success(userService.deleteUser(id));
    }
    
    // Vehicle management
    @GetMapping("/vehicles")
    public Result<List<Vehicle>> getAllVehicles() {
        return Result.success(vehicleService.getAllVehicles());
    }
    
    @GetMapping("/vehicles/{id}")
    public Result<Vehicle> getVehicleById(@PathVariable Long id) {
        return Result.success(vehicleService.findById(id));
    }

    @PutMapping("/vehicles/{id}")
    public Result<Boolean> updateVehicle(@PathVariable Long id, @RequestBody Vehicle vehicle) {
        Vehicle existing = vehicleService.getById(id);
        // 当状态变更为维修状态时更新时间（使用正确状态常量）
        if (vehicle.getStatus() == Vehicle.STATUS_MAINTENANCE && existing.getStatus() != Vehicle.STATUS_MAINTENANCE) {
            // 使用LocalDateTime.now()替代Date
            vehicle.setLastMaintenanceTime(LocalDateTime.now());
        }
        return Result.success(vehicleService.updateById(vehicle));
    }
    
    @DeleteMapping("/vehicles/{id}")
    public Result<Boolean> deleteVehicle(@PathVariable Long id) {
        return Result.success(vehicleService.deleteVehicle(id));
    }
    
    // Shop management
    @GetMapping("/shops")
    public Result<List<CarRentalShop>> getAllShops() {
        return Result.success(carRentalShopService.list());
    }
    
    // Order management
    @GetMapping("/orders")
    public Result<List<RentalOrder>> getAllOrders() {
        return Result.success(rentalOrderService.getAllOrders());
    }
    
    @GetMapping("/orders/{id}")
    public Result<RentalOrder> getOrderById(@PathVariable Long id) {
        return Result.success(rentalOrderService.findById(id));
    }
    
    @PutMapping("/orders/{id}/status")
    public Result<Boolean> updateOrderStatus(@PathVariable Long id, @RequestParam String status) {
        return Result.success(rentalOrderService.updateOrderStatus(id, status));
    }
} 